package views;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;

import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;











import core.logic.services.utilServices.ValidatieService;
import core.models.dataModels.createModels.ToevoegenBetalingsGegModel;
import core.models.dataModels.createModels.ToevoegenBookingPersGegModel;
import core.models.domainModel.Passagier;

import javax.swing.JButton;

import controllers.createControllers.ToevoegenPersoneelController;
import controllers.uiControllers.PortalController;
import controllers.updateControllers.UpdatePassagierController;


public class UpdatePassaViewFull extends JFrame {
	private JTextField txtNaam;
	private JTextField txtVoornaam;
	private JTextField txtStraatNaam;
	private JTextField txtBus;
	private JTextField txtHuisNr;
	private JTextField txtIdNr;
	private JTextField txtKaartNr;
	private JTextField txtVervaldatum;
	private JTextField txtCsv;
	private JTextField txtGeboorteDatum;
	private JTextField txtGemeente;
	private JTextField txtPostcode;
	
	
	
	public UpdatePassaViewFull(ToevoegenBookingPersGegModel model2, ToevoegenBetalingsGegModel model1) {
		
		final UpdatePassaViewFull ditScherm = this;
		
		ToevoegenBetalingsGegModel modelBetaling = model1;
		final ToevoegenBookingPersGegModel modelPers = model2;


		JLabel lblTitel = new JLabel("Passagier Wijzigen");
		lblTitel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
		
		JLabel lblPersGeg = new JLabel("Persoonsgegevens");
		lblPersGeg.setFont(new Font("Tahoma", Font.ITALIC, 14));
		
		JLabel lblNaam = new JLabel("Naam");
		
		JLabel lblVn = new JLabel("Voornaam");
		
		JLabel lblNewLabel = new JLabel("Straat");
		
		txtNaam = new JTextField();
		txtNaam.setColumns(10);
		
		txtVoornaam = new JTextField();
		txtVoornaam.setColumns(10);
		
		txtStraatNaam = new JTextField();
		txtStraatNaam.setColumns(10);
		
		JLabel lblBus = new JLabel("Bus");
		
		txtBus = new JTextField();
		txtBus.setColumns(10);
		
		JLabel lblNr = new JLabel("Nr.");
		
		txtHuisNr = new JTextField();
		txtHuisNr.setColumns(10);
		
		JLabel lblIdentiteitskaartnr = new JLabel("ID nr.");
		
		txtIdNr = new JTextField();
		txtIdNr.setColumns(10);
		
		JLabel lblBetalingsgegevens = new JLabel("Betalingsgegevens");
		lblBetalingsgegevens.setFont(new Font("Tahoma", Font.ITALIC, 14));
		
		JLabel lblKaarttype = new JLabel("Kaarttype");
		
		final JComboBox<String> cBoxCardType = new JComboBox<String>();
		
		for (String kaartType : modelBetaling.getCreditCardList()) {
			cBoxCardType.addItem(kaartType);
		}
		
		JLabel lblKaartnr = new JLabel("KaartNr.");
		
		txtKaartNr = new JTextField();
		txtKaartNr.setColumns(10);
		
		JLabel lblVervaldatum = new JLabel("Vervaldatum");
		
		txtVervaldatum = new JTextField();
		txtVervaldatum.setColumns(10);
		
		JLabel lblCvsCode = new JLabel("Cvs code");
		
		txtCsv = new JTextField();
		txtCsv.setColumns(10);
		
		JLabel lblGeboortedatum = new JLabel("Geboortedt");
		
		txtGeboorteDatum = new JTextField();
		txtGeboorteDatum.setColumns(10);
		
		JLabel lblGemeente = new JLabel("Gemeente");
		
		txtGemeente = new JTextField();
		txtGemeente.setColumns(10);
		
		JLabel lblPostcode = new JLabel("Postcode");
		
		txtPostcode = new JTextField();
		txtPostcode.setColumns(10);
		
		JLabel lblNationaliteit = new JLabel("Nationaliteit");
		
		final JComboBox<String> cBoxNationaliteiten = new JComboBox<String>();
		Collections.sort(modelPers.getNationaliteiten());
		for(String nation:modelPers.getNationaliteiten()){
			cBoxNationaliteiten.addItem(nation);
		}
		
	
		
		JButton btnSave = new JButton("Bewaren");
		
		btnSave.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) 
			{														
				if (Fieldvalidation(ditScherm))	
				{
					modelPers.getPassagier().getPersoon().setNaam(txtNaam.getText());
					modelPers.getPassagier().getPersoon().setVoornaam(txtVoornaam.getText());
					modelPers.getPassagier().getPersoon().setGeboortedatum(txtGeboorteDatum.getText());
					modelPers.getPassagier().getPersoon().getAdres().setBus(txtBus.getText());
					modelPers.getPassagier().getPersoon().getAdres().setGemeente(txtGemeente.getText());
					modelPers.getPassagier().getPersoon().getAdres().setHuisnummer(Integer.parseInt(txtHuisNr.getText()));
					modelPers.getPassagier().getPersoon().getAdres().setLand(cBoxNationaliteiten.getSelectedItem().toString());
					modelPers.getPassagier().getPersoon().getAdres().setPostcode(txtPostcode.getText());
					modelPers.getPassagier().getPersoon().getAdres().setStraat(txtStraatNaam.getText());
					modelPers.getPassagier().setPaspoortNr(txtIdNr.getText());
					modelPers.getPassagier().setKaartnummer(txtKaartNr.getText());
					modelPers.getPassagier().setKaartType(cBoxCardType.getSelectedItem().toString());
					modelPers.getPassagier().setVervaldatumKaart(txtVervaldatum.getText());
					modelPers.getPassagier().setControleCijfer(Integer.parseInt(txtCsv.getText()));
				}
				
				UpdatePassagierController.handleView(modelPers);

			}
		});
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(36)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(lblCvsCode)
						.addComponent(lblVervaldatum)
						.addComponent(lblKaartnr)
						.addComponent(lblKaarttype)
						.addComponent(lblIdentiteitskaartnr)
						.addComponent(lblGemeente)
						.addComponent(lblPostcode)
						.addComponent(lblBus)
						.addComponent(lblNr)
						.addComponent(lblNewLabel)
						.addComponent(lblVn)
						.addComponent(lblPersGeg, GroupLayout.PREFERRED_SIZE, 145, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblTitel, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnSave, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNaam)
								.addComponent(lblGeboortedatum))
							.addGap(31)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(txtVoornaam, 310, 310, 310)
								.addComponent(txtNaam, GroupLayout.PREFERRED_SIZE, 310, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtStraatNaam, 310, 310, 310)
								.addComponent(txtHuisNr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtBus, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtPostcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtGemeente, 310, 310, 310)
								.addComponent(txtGeboorteDatum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtIdNr, GroupLayout.PREFERRED_SIZE, 310, GroupLayout.PREFERRED_SIZE)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
									.addComponent(cBoxNationaliteiten, 0, 208, Short.MAX_VALUE)
									.addGroup(groupLayout.createSequentialGroup()
										.addPreferredGap(ComponentPlacement.RELATED)
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
											.addComponent(txtKaartNr)
											.addComponent(cBoxCardType, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
											.addComponent(txtVervaldatum)
											.addComponent(txtCsv, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))))
						.addComponent(lblBetalingsgegevens, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNationaliteit))
					.addGap(20))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(63)
							.addComponent(txtNaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblTitel)
										.addComponent(btnSave))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(lblPersGeg)
									.addGap(50)
									.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblVn)
										.addComponent(txtVoornaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblNewLabel)
										.addComponent(txtStraatNaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblNr)
										.addComponent(txtHuisNr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblBus)
										.addComponent(txtBus, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblPostcode)
										.addComponent(txtPostcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblGemeente)
										.addComponent(txtGemeente, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(63)
									.addComponent(lblNaam)))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblIdentiteitskaartnr)
								.addComponent(txtIdNr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(txtGeboorteDatum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblGeboortedatum))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNationaliteit)
								.addComponent(cBoxNationaliteiten, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addGap(18)
					.addComponent(lblBetalingsgegevens, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblKaarttype)
						.addComponent(cBoxCardType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblKaartnr)
						.addComponent(txtKaartNr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblVervaldatum)
						.addComponent(txtVervaldatum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCvsCode)
						.addComponent(txtCsv, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
		
		
		
		//code
		//velden opvullen
		txtNaam.setText(modelPers.getPassagier().getPersoon().getNaam());
		txtIdNr.setText(modelPers.getPassagier().getPaspoortNr());		
		txtKaartNr.setText(modelBetaling.getKaartNummer());
		txtBus.setText(modelPers.getPassagier().getPersoon().getAdres().getBus());
		txtGeboorteDatum.setText(modelPers.getPassagier().getPersoon().getGeboortedatum());
		txtGemeente.setText(modelPers.getPassagier().getPersoon().getAdres().getGemeente());
		txtHuisNr.setText(Integer.toString(modelPers.getPassagier().getPersoon().getAdres().getHuisnummer()));
		txtPostcode.setText(modelPers.getPassagier().getPersoon().getAdres().getPostcode());
		txtVoornaam.setText(modelPers.getPassagier().getPersoon().getVoornaam());
		txtStraatNaam.setText(modelPers.getPassagier().getPersoon().getAdres().getStraat());
		txtVervaldatum.setText(modelBetaling.getVervalDatum());
		txtCsv.setText(Integer.toString(modelBetaling.getCvsCode()));
		
		for (int i=0; i < cBoxNationaliteiten.getItemCount(); i++)
		{
			String t1 = modelPers.getNationaliteit();
			String t2 = cBoxNationaliteiten.getItemAt(i);
			if (t1.equals(t2))
			{
				cBoxNationaliteiten.setSelectedIndex(i);
			}
		}
		
		for (int i=0; i < cBoxCardType.getItemCount(); i++)
		{
			String t1 = modelBetaling.getKaartType();
			String t2 = cBoxCardType.getItemAt(i);
			if (t1.equals(t2))
			{
				cBoxCardType.setSelectedIndex(i);
			}
		}
				

	}
	
	public boolean Fieldvalidation(UpdatePassaViewFull scherm)
	{
		Boolean correct = true;
		
		try
		{
			ValidatieService.isPresent(scherm.txtNaam, "Naam ");
		}
		catch (Exception exc)
		{
			correct = false;
		}
		try
		{
			ValidatieService.isPresent(scherm.txtVoornaam, "Voornaam ");
		}
		catch (Exception exc)
		{
			correct = false;
		}
		try
		{
			ValidatieService.isPresent(scherm.txtGeboorteDatum, "Geboortedatum ");
		}
		catch (Exception exc)
		{
			correct = false;
		}
		try
		{
			ValidatieService.isPresent(scherm.txtGemeente, "Gemeente ");
		}
		catch (Exception exc)
		{
			correct = false;
		}
		try
		{
			ValidatieService.isPresent(scherm.txtPostcode, "Postcode ");
		}
		catch (Exception exc)
		{
			correct = false;
		}
		try
		{
			ValidatieService.isPresent(scherm.txtStraatNaam, "Straatnaam ");
		}
		catch (Exception exc)
		{
			correct = false;
		}
		try
		{
			ValidatieService.isPresent(scherm.txtKaartNr, "Kaartnummer ");
		}
		catch (Exception exc)
		{
			correct = false;
		}
		try
		{
			ValidatieService.isPresent(scherm.txtVervaldatum, "Vervaldatum kaart ");
		}
		catch (Exception exc)
		{
			correct = false;
		}
		try
		{
			ValidatieService.isPresent(scherm.txtIdNr, "IdentiteitskaartNummer  ");
		}
		catch (Exception exc)
		{
			correct = false;
		}
		try
		{
			ValidatieService.isInteger(scherm.txtHuisNr, "Huisnummer ");
		}
		catch (Exception exc)
		{
			correct = false;
		}
		try
		{
			ValidatieService.isInteger(scherm.txtCsv, "Csv ");
		}
		catch (Exception exc)
		{
			correct = false;
		}
		
		return correct;
	}
}
